package com.example.lot_lock;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static java.lang.Thread.sleep;

public class login extends AppCompatActivity {
    private ToggleButton toggleButton=null;//小眼睛
    private EditText editText_name = null;//用户名录入编辑栏
    private EditText editText_pass = null;//密码录入编辑栏
    private ImageButton button_login = null;//登录
    private Button   button_register = null;//注册
    private Button   button_forget = null;//忘记密码

    ApplicationTCP applicationTCP = null;
    InputStream inputStream = null;
    OutputStream outputStream = null;

    String str = null;

    Handler handler = new Handler(){
        public  void handleMessage(Message msg){
            switch (msg.what){
                case '0'://用户名未注册
                    noName();
                    break;
                case '1'://密码正确
                    try {
                        passRight();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    //Toast.makeText(login.this, "passRight", Toast.LENGTH_SHORT).show();
                    break;
                case '2'://密码错误
                    passError();
                default:
            }
        }
    };
private void noName() {
    AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
    builder.setTitle("用户未注册").setIcon(R.mipmap.one_image).setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    AlertDialog ad = builder.create();
    ad.show();
    }

private void passError(){
    AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
    builder.setTitle("密码错误").setIcon(R.mipmap.one_image).setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    AlertDialog ad = builder.create();
    ad.show();
}
private  void passRight() throws InterruptedException {

     applicationTCP.userNmae = editText_name.getText().toString();//账户名导入
     //applicationTCP.updateEquipState();//获取设备ID和在线状态
     enteradd_change_equipActivity();//页面跳转
}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        login_Init();
//控件初始化
        toggleButton = findViewById(R.id.login_eye_id);//小眼睛
        editText_pass = findViewById(R.id.user_pass_id);//密码编辑栏
        editText_name = findViewById(R.id.user_name_id);
        button_login = findViewById(R.id.login_id);
        button_register = findViewById(R.id.register_id);
        button_forget = findViewById(R.id.forget_id);

//-------------------监听登录按钮
        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            sendData();//发送登录请假
            }
        });

//-------------------监听注册按钮
        button_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(login.this, register.class);
                startActivity(intent);
            }
        });
//------------------监听忘记密码按钮
        button_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(login.this, forget.class);
                startActivity(intent);
            }
        });

//监听ToggleButton控件--小眼睛
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SetPassCloseoropen(isChecked);
            }
        });
    }
   void enteradd_change_equipActivity(){
    Intent intent = new Intent(login.this, add_change_equip.class);
    startActivity(intent);
    }
    //--------------发送登录信号到服务器-----------
    public void sendData(){
        String dataSend = '2'+editText_name.getText().toString() + "+" + editText_pass.getText().toString() +'\n';
        final byte []receiveData = new byte[1024];
        new Thread(new Runnable() {//发送线程
            @Override
            public void run() {
                try {
                    outputStream.write(dataSend.getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                new Thread(new Runnable() {//接受线程
                    @Override
                    public void run() {
                        Message msg = new Message();
                        try {
                            inputStream.read(receiveData,0,1);
                            msg.what = receiveData[0];
                            handler.sendMessage(msg);
                        } catch (IOException e) {
                        }
                    }
                }).start();
            }
        }).start();
    }
//------------进入APP----------------

    private void login_Init() {//建立网络通讯
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    applicationTCP = (ApplicationTCP)login.this.getApplication();
                    applicationTCP.Init();
                    outputStream = applicationTCP.getOut();
                    inputStream  = applicationTCP.getIn();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //密码显示状态
    private void SetPassCloseoropen(Boolean flag){
        //设置小眼睛图标
        toggleButton.setBackgroundResource(flag?R.mipmap.openeye : R.mipmap.closeeye);
        //设置EIDT-inputtype
        editText_pass.setInputType(flag?(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_NORMAL):(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD));
        //移动光标
        CharSequence charSequence = editText_pass.getText();
        if(charSequence instanceof Spannable){
            Spannable spanText = (Spannable)charSequence;
            Selection.setSelection(spanText, charSequence.length());
        }
    }

}
